package com.icesoft.x.demo;

import java.util.List;
import java.util.ArrayList;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.ServletContext;

import com.icesoft.AppState;
import com.icesoft.faces.async.render.SessionRenderer;
import com.icesoft.faces.context.effects.JavascriptContext;
import com.mcv.x.lists.SelectList;

public class ArtshowBean {

    private List data = new ArrayList();
    private List<String> images = new ArrayList<String>();
    private ArtShowDAO artShowDAO = new ArtShowDAO();
    private int selectedIndex=0;
    private String selectedImage;
    private String artist;
    private static final AppState appState = AppState.getInstance();

    /**
	 * @return the artist
	 */
	public String getArtist() {
		return artist;
	}



	/**
	 * @param artist the artist to set
	 */
	public void setArtist(String artist) {
		this.artist = artist;
	}

	FacesContext facesContext = FacesContext.getCurrentInstance();
    public void actionListener(ActionEvent e){
    	SelectList component  = (SelectList) e.getSource();
    	selectedIndex = component.getSelectedIndex();
    	ArtShowItem item = (ArtShowItem) data.get(selectedIndex);
    	selectedImage = 
    		"./images/100044/"+images.get(selectedIndex)+"/medium.jpg";
        System.out.println("Row [" + selectedImage + "]");
        artist = item.getArtist();
        JavascriptContext.addJavascriptCall(facesContext, "goToView2(event)");
        
    }
    
    
    
    /**
	 * @return the selectedImage
	 */
	public String getSelectedImage() {
		return selectedImage;
	}
	

	/**
	 * @param selectedImage the selectedImage to set
	 */
	public void setSelectedImage(String selectedImage) {
		this.selectedImage = selectedImage;
	}


	public ArtshowBean() {
		
		AppState.getInstance();
		FacesContext facesContext = FacesContext.getCurrentInstance();
	    ServletContext servletContext = (ServletContext) facesContext.getExternalContext().getContext();
		String path = servletContext.getRealPath("/");
		AppState.setCertificateRealPath(path);
		
		/*if (!appState.isStateInit()) {
        String b = "Art by Gabz Larose 2009";
       images = artShowDAO.filldata();
        for(int i = 0; i < images.size()-1;i++){
            data.add(new ArtShowItem(images.get(i), b, "./images/100044/"+images.get(i)+"/msquare.jpg",null,null));
        }
        appState.setArt(data);
        }*/
		   SessionRenderer.addCurrentSession("all");
    }


    public List getData() {
    	System.out.println("sessionCount " + appState.getSessionCount());
        return appState.getData();
    }

    public void setData(List data) {
        this.data = data;
    }


    
}
